package com.ssm.service.impl;

import com.ssm.common.Constants;
import com.ssm.dao.ArticleDao;
import com.ssm.entity.Article;
import com.ssm.redis.RedisUtil;
import com.ssm.service.ArticleService;
import com.ssm.utils.AntiXssUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by LinYaTing on 2018/12/9 23:54
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    private static final Logger log = Logger.getLogger(ArticleService.class);

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Article> findArticle(Map<String, Object> map) {
        return articleDao.findArticle(map);
    }

    @Override
    public Long getTotalArticle(Map<String, Object> map) {
        return articleDao.getTotalArticle(map);
    }

    @Override
    public Integer addArticle(Article article) {
        if (article.getArticleTitle() == null || article.getArticleContent() == null || getTotalArticle(null) > 90 || article.getArticleContent().length() > 50000) {
            return 0;
        }
        article.setArticleTitle(AntiXssUtil.replaceHtmlCode(article.getArticleTitle()));
        if (articleDao.insertArticle(article) > 0) {
            log.info("insert article success,save article to redis");
            redisUtil.put(Constants.ARTICLE_CACHE_KEY + article.getId(),article);
            return 1;
        }
        return 0;
    }
}
